package cn.dly.login.service.impl;

import cn.dly.login.entity.User;
import cn.dly.login.exception.LRException;
import cn.dly.login.serurity.entity.SecurityUser;
import cn.dly.login.service.AuthService;
import cn.dly.login.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserDetailsServiceImpl implements org.springframework.security.core.userdetails.UserDetailsService {

    @Autowired
    private UserService userService;

    @Autowired
    private AuthService authService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user=userService.selectByUsername(s);
        if(user==null){
            throw new LRException(20001,"error");
        }
        cn.dly.login.serurity.entity.User curUser=new cn.dly.login.serurity.entity.User();
        BeanUtils.copyProperties(user,curUser);
        List<String> authorities=authService.selectAuthValueByUserId(s);
        SecurityUser securityUser=new SecurityUser(curUser);
        securityUser.setPermissionValueList(authorities);
        return securityUser;
    }
}
